package com.ce.server.controller;


import com.ce.server.common.RespBean;
import com.ce.server.common.RespPageBean;
import com.ce.server.pojo.Admin;
import com.ce.server.pojo.Hscheck;
import com.ce.server.pojo.Operlog;
import com.ce.server.service.IHscheckService;
import com.ce.server.service.IOperlogService;
import com.ce.server.util.AdminUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Arrays;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 郑爽
 * @since 2021-11-17
 */
@RestController
@RequestMapping("/hscheck")
public class HscheckController {

  @Autowired
  IHscheckService hscheckService;
  @Autowired
  private IOperlogService operlogService;

  @Value("${type.student}")
  private String type;


  @ApiOperation(value = "根据id批量删除核酸记录,一般用于删除多余的核酸记录")
  @DeleteMapping("/")
  public RespBean deleteHsCheckByIds(Integer[] ids) {

    if (hscheckService.removeByIds(Arrays.asList(ids))) {
      Admin currentAdmin = (Admin) AdminUtil.getCurrentAdmin();
      operlogService.save(new Operlog().setWno(currentAdmin.getWno())
              .setWname(currentAdmin.getWname())
              .setOper("根据id批量删除核酸记录,一般用于删除多余的核酸记录").setOpertime(LocalDateTime.now()));
      return RespBean.success("删除成功!");
    }
    return RespBean.error("删除失败!");
  }

  @ApiOperation(value = "核酸人员根据核酸结果录入信息并修改学生jkm信息")
  @PutMapping("/")
  public RespBean writeHsResultInToTable(Integer[] ids, String sno, String hsResult) {
    Admin currentAdmin = (Admin) AdminUtil.getCurrentAdmin();
    operlogService.save(new Operlog().setWno(currentAdmin.getWno())
            .setWname(currentAdmin.getWname())
            .setOper("根据核酸结果录入信息并修改学生jkm信息").setOpertime(LocalDateTime.now()));
    return hscheckService.writeHsResultInToTable(ids, sno, hsResult);
  }

  @ApiOperation(value = "分页处理获取所有的核酸记录")
  @GetMapping("/")
  public RespPageBean getStudentsByPage(@RequestParam(defaultValue = "1") Integer currentPage,
                                        @RequestParam(defaultValue = "10") Integer size,
                                        Hscheck hscheck, LocalDate[] createTimeScope, LocalDate[] hsResultScope, String sname) {
    return hscheckService.getHsChecksByPage(currentPage, size, hscheck, createTimeScope, hsResultScope, sname);
  }


}
